package com.ugrow.ugrowcommunity.activity.dormitoryselection.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.courseevaluation.EvaluationActivity;
import com.ugrow.ugrowcommunity.activity.dormitoryselection.bean.DormitoryBean;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.ItemDormitoryBinding;
import com.ugrow.ugrowcommunity.util.HttpUtil;
import com.ugrow.ugrowcommunity.util.ToastUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;


public class DormitoryAdapter extends RecyclerView.Adapter<DormitoryAdapter.ViewHolder>{



    private Activity context;
    private ArrayList<DormitoryBean.Data> arrayList;

    public void setContext(Activity context) {
        this.context = context;
    }

    public void setArrayList(ArrayList<DormitoryBean.Data> arrayList) {
        this.arrayList = arrayList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ItemDormitoryBinding itemDormitoryBinding;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemDormitoryBinding = ItemDormitoryBinding.bind(itemView);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dormitory,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.itemDormitoryBinding.tvDormitoryname.setText(arrayList.get(position).getDormitoryno()+"");
        holder.itemDormitoryBinding.tvNumberseats.setText("当前剩余床位"+arrayList.get(position).getCapacity());
        holder.itemDormitoryBinding.layoutOnclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(arrayList.get(position).getDormitoryno());
            }
        });
    }

    private void showDialog(int dormitoryno){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("宿舍选择后不可更改，确认更改吗？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                HashMap hashMap = new HashMap();
//                hashMap.put("dormitoryno",dormitoryno);
//                HttpUtil.httpPost("http://82.157.173.219:8080/SmartCampus/dor/selectAllBySex", hashMap, HttpUtil.ContentType.FROM, new HttpUtil.CallBack() {
//                    @Override
//                    public void success(HttpUtil.Result result) {
//                        ToastUtil.ToastShort(context,"预约成功");
//                    }
//
//                    @Override
//                    public void failed(IOException e) {
//                        Log.d("TAG", "宿舍预约失败");
//                    }
//                });
                ToastUtil.ToastShort(context,"预约成功");
                context.finish();
            }
        });
        builder.create().show();
    }

    @Override
    public int getItemCount() {
        if(arrayList == null){
            return 0;
        }
            return arrayList.size();


    }


}
